﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

// For this menu class I have reused code from a previous project I have worked on


namespace InfinateSki
{
    class Menu{
    

        private List<MenuItem> itemList;
        private int selectedItem;
        private SpriteFont spriteFont;
        private string menuTitle;
        private Texture2D backgroundImage;

        struct MenuItem
        {
            public string itemText;
            public Menu itemLink;
            public MenuItem(string itemText, Menu itemLink)
            {
                this.itemText = itemText;
                this.itemLink = itemLink;
            }

        }

        public Menu(SpriteFont spriteFont, string menuTitle, Texture2D backgroundImage)
        {
            itemList = new List<MenuItem>();
            selectedItem = 0;
            this.spriteFont = spriteFont;
            this.menuTitle = menuTitle;
            this.backgroundImage = backgroundImage;

        }

        public void AddMenuItem(string itemText, Menu itemLink)
        {
            MenuItem newItem = new MenuItem(itemText, itemLink);
            itemList.Add(newItem);
            
        }

        public Menu ProcessInput(KeyboardState lastKeyState, KeyboardState currKeyState)
        {
            //Go down a menu item by increasing the selectedItem value
            if (lastKeyState.IsKeyUp(Keys.Down) && currKeyState.IsKeyDown(Keys.Down))
                selectedItem++;
            
            //Go up a menu item by decreasing the selectedItem value
            if (lastKeyState.IsKeyUp(Keys.Up) && currKeyState.IsKeyDown(Keys.Up))
                selectedItem--;

            // Stop selectedItem from going below 0
            if (selectedItem < 0) 
                selectedItem = 0;

            // Stop selectedItem from going above the amount of items in the Menu
            if(selectedItem > itemList.Count)
            {
                selectedItem = itemList.Count - 1;
            }

            //
            if ((lastKeyState.IsKeyUp(Keys.Enter) && currKeyState.IsKeyDown(Keys.Enter)))
                return itemList[selectedItem].itemLink;
            else if (lastKeyState.IsKeyDown(Keys.Escape))
                return null;
            else
                return this;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            float xPos = 250;
            float yPos = 300;
            //draw background image
            spriteBatch.Draw(backgroundImage, new Vector2(0, 0), null, Color.White, 0, new Vector2(0, 0), new Vector2(1, 1), SpriteEffects.None, 0.5f);
            spriteBatch.DrawString(spriteFont, menuTitle, new Vector2(xPos, 200), Color.White, 0, new Vector2(0, 0), 1.5f, SpriteEffects.None, 0);
            for (int itemID = 0; itemID < itemList.Count; itemID++)
            {
                Vector2 itemPos = new Vector2(xPos, yPos);
                Color itemColor = Color.White;
                if (itemID == selectedItem)
                    itemColor = new Color(new Vector4(0, 0, 1, 1));
                else
                    itemColor = new Color(new Vector4(1, 1, 1, 1));
                spriteBatch.DrawString(spriteFont, itemList[itemID].itemText, itemPos, itemColor, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
                yPos += 30;
            }
        }





    }
}
